package student.web.internal;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import cloudspace.config.CloudSpaceConfiguration;
import cloudspace.stats.StatisticsService;
import cloudspace.vm.io.console.CometExecutor;


public class CloudSpaceContextListener implements ServletContextListener
{

    public void contextDestroyed( ServletContextEvent arg0 )
    {
        CometExecutor.shutdown();
        StatisticsService.getStatService().persistAll();
    }


    public void contextInitialized( ServletContextEvent arg0 )
    {
        try
        {
            if ( !TemplateManager.getInstance().isEnabled() )
            {
                File store = CloudSpaceConfiguration.getInstance()
                    .getStorageLocation();
                if ( !store.exists() )
                    CloudSpaceConfiguration.getInstance().initializeStore();
                File storageLocation = new File( store, "template/" );
                if ( !storageLocation.exists() )
                    storageLocation.mkdir();
                TemplateManager.getInstance()
                    .enableTemplateChecking( storageLocation );
            }
        }
        catch ( IOException e )
        {
            System.err.println( "Could not create storage directory" );
            e.printStackTrace();
        }
    }

}
